package archforce.atp.x.ui.impl.left;

import archforce.atp.x.ConnectConfigView;
import archforce.atp.x.ui.frame.component.FacedComponent;
import archforce.atp.x.ui.impl.right.IPPortMappingItemComponent;
import archforce.atp.x.ui.impl.right.IPPortMappingModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;

/**
 * @author wx
 * @version 1.0
 * @date 2022/4/12 15:26
 */
public class SelectActionListener implements ActionListener {

    private AtomicBoolean isAutoModel;
    private JPanel cardContainer;
    private Map<String, FacedComponent> autoModelFacedComponentMap;
    private Function<FacedComponent, ScrollPane> function;
    private Function<String, String> keyFunction;

    /**
     * 左侧面板行选项监听器
     * @param isAutoModel 是否匹配模式
     * @param cardContainer 匹配模式面板的内容容器，左侧每一个行对应匹配模式的一个card
     * @param autoModelFacedComponentMap 匹配模式的所有card的存放容器，用来获取card
     * @param function 获取要添加到的scrollPane的方法，每一个card都备一个scrollPane包裹,需要将这个scrollPane添加到容器
     */
    public SelectActionListener(AtomicBoolean isAutoModel, JPanel cardContainer, Map<String, FacedComponent> autoModelFacedComponentMap, Function<FacedComponent, ScrollPane> function, Function<String,String> keyFunction) {
        this.isAutoModel = isAutoModel;
        this.cardContainer = cardContainer;
        this.autoModelFacedComponentMap = autoModelFacedComponentMap;
        this.function = function;
        this.keyFunction = keyFunction;
    }

    /**
     * 左侧面板行选项监听器
     * @param isAutoModel 是否匹配模式
     * @param cardContainer 匹配模式面板的内容容器，左侧每一个行对应匹配模式的一个card
     * @param autoModelFacedComponentMap 匹配模式的所有card的存放容器，用来获取card
     */
    public SelectActionListener(AtomicBoolean isAutoModel, JPanel cardContainer, Map<String, FacedComponent> autoModelFacedComponentMap, Function<String,String> keyFunction) {
        this.isAutoModel = isAutoModel;
        this.cardContainer = cardContainer;
        this.autoModelFacedComponentMap = autoModelFacedComponentMap;
        this.function = ConnectConfigView::getAutoModelScrollPanel;
        this.keyFunction = keyFunction;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String index = e.getActionCommand();
        String key = this.keyFunction.apply(index);
        System.out.println("选择了：" + key);
        if(isAutoModel.get()){
            FacedComponent facedComponent = autoModelFacedComponentMap.get(key);
            if (facedComponent == null) {
                FacedComponent<IPPortMappingItemComponent, IPPortMappingModel> instance = FacedComponent.getInstance(IPPortMappingItemComponent.class);
                ScrollPane apply = function.apply(instance);
                autoModelFacedComponentMap.putIfAbsent(key, instance);
                cardContainer.add(apply, key);
            }
            ((CardLayout)cardContainer.getLayout()).show(cardContainer, key);

        }
    }
}
